package com.tfkj.contrller;

import com.tfkj.entity.Student;
import com.tfkj.entity.Suggestion;
import com.tfkj.service.AllService;
import com.tfkj.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
@RestController
@CrossOrigin(maxAge = 3600)
@RequestMapping("/student")
@Api(value = "学生", tags = "学生接口")
public class StudentController {

    @Autowired
    private AllService allService;

    @ApiOperation("学生列表")
    @PostMapping("/list")
    public CommonReturn userList(@RequestParam("role") String role, @RequestParam(value = "className",required = false) String className, @RequestParam(value = "name", required = false) String name, @RequestParam("pageNum")Integer pageNum, @RequestParam("pageSize")Integer pageSize) {
        List<Student> users = allService.findAllUser(role, className, name, pageNum, pageSize);
        Integer total = allService.getStudentTotal(role, className, name);
        return new CommonReturn(users,total);
    }



    @ApiOperation("查看老师列表")
    @GetMapping("/teacherList")
    public CommonReturn suggestionList(@RequestParam("className") String className) {
        List<Student> list = allService.findTeacherList(className);
        List<KeyValue> res = new ArrayList<>();
        if (list != null) {
            for (Student teacher:list) {
                KeyValue r = new KeyValue(teacher.getStudentNum(), teacher.getName());
                res.add(r);
            }
        }
        return new CommonReturn(res);
    }


    @ApiOperation("创建用户（管理员创建老师，老师创建学生）")
    @PostMapping("/create")
    public CommonReturn createUser(@RequestBody Student student) {
        allService.createUser(student);
        return new CommonReturn(200, "创建成功！");
    }

    @ApiOperation("登录接口")
    @PostMapping("/login")
    public CommonReturn login(@RequestBody LoginVO loginVO, HttpSession session) {
        Student user = allService.login(loginVO.getStudentNum(), loginVO.getPassword());
        if (user == null) {
            return new CommonReturn(500, "学号或密码错误！");
        }
        session.setAttribute("studentNum", user.getStudentNum());
        session.setAttribute("name", user.getName());
        System.out.println("studentNum:"+user.getId());
        return new CommonReturn(user);
    }
    @ApiOperation("退出登录")
    @GetMapping("/logout")
    public CommonReturn logout(HttpSession session) {
        session.removeAttribute("studentNum");
        session.removeAttribute("name");
        return new CommonReturn(200, "退出成功！");
    }


    @ApiOperation("修改个人信息")
    @PostMapping("/updateInfo")
    public CommonReturn updateInfo(@RequestBody UpdateUserInfoVO vo) {
        allService.updateUserInfo(vo.getStudentNum(), vo.getPhone(), vo.getEmail());
        return new CommonReturn(200, "修改成功！");
    }

    @ApiOperation("修改密码")
    @PostMapping("/update")
    public CommonReturn updateUser(@RequestBody UpdateUserVO vo) {
        Integer i = allService.updateUser(vo.getStudentNum(), vo.getPassword(), vo.getOldPassword());
        if (i == 0) {
            return new CommonReturn(500, "旧密码不正确！");
        }
        return new CommonReturn(200, "修改成功！");
    }

}
